<template>
	<view class="content">
		<view class = "login_from">
			<view class="login_from_input">
				<view class="login_from_name">账号名</view>
				<view class="login_from_fun"><input type="text" v-model="name" placeholder="请输入账号名"></view>
			</view>
			
			<view class="login_from_input">
				<view class="login_from_name">密码</view>
				<view class="login_from_fun"><input type="digit" v-model="newPassword" placeholder="请输入新密码"/></view>
			</view>
			
			<view class="login_from_input">
				<view class="login_from_name">确认</view>
				<view class="login_from_fun"><input type="digit" v-model="rePassword" placeholder="请输入确认密码"/></view>
			</view>
			
			<view class="login_from_input">
				<view class="login_from_name">验证码</view>
				<view class="login_from_fun">
					<input style="width: 40%; text-align: left" type="digit" v-model="smsCode" maxlength="6" placeholder="验证码">
					<label class="regFrom_tom_yzlabel" :style="{ color : QzyzmStare?'#cccccc':'#2ebbfe'}" @click="Qzyzm">{{Qztime}}{{Qztext}}</label>
				</view>
			</view>

			<view class="login_from_dl">
				<button @click="chongzhi">重置密码</button>
			</view>
			<uni-popup ref="alertDialog" type="dialog">
				<uni-popup-dialog :type="msgType" cancelText="关闭" @confirm="backTologin" confirmText="确认" title="提示信息" :content="messageText"></uni-popup-dialog>
			</uni-popup>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				name: "",
				smsCode: "",
				newPassword: "",
				rePassword: "",
				gouxSta:false,
				Qztime:'',
				QzyzmStare:false,
				Qztext:'获取验证码',
				msgType:"",
				messageText:""
			}
		},
    methods: {
		backTologin(){
			if(this.messageText == "修改成功"){
				uni.navigateTo({
					url:"/pages/login/login"
				})
			}
		},
		showError(msg){
			this.msgType = "error";
			this.messageText = msg;
			this.$refs.alertDialog.open()
		},
		showSuccess(msg){
			this.msgType = "success";
			this.messageText = msg;
			this.$refs.alertDialog.open()
		},
		chongzhi(){
			if(this.password!=this.confirmPassword){
				this.showError("密码不一致")
			}else{
				uni.request({
					url:"http://localhost:8080/user/forget",
					method:'POST',
					data:{
						name: this.$encryptByPublicKey(this.name),
						newPassword: this.$encryptByPublicKey(this.newPassword),
						sms: this.$encryptByPublicKey(this.smsCode)
					},
					header:{
						'Content-Type':'application/x-www-form-urlencoded' //请求头信息
					},
					success:(res)=>{
						if(res.data=="修改成功"){
							this.showSuccess("修改成功");
						}
						else{
							this.showError(res.data);
						}
					},
					fail:()=> {
						console.log("调用失败")
					}
				})
			}
		},
		Qzyzm(){
					var num = 60;
					if( this.QzyzmStare == false){
						uni.request({
							url:"http://localhost:8080/user/forget/sms",
							method:'POST',
							data:{	
								name: this.$encryptByPublicKey(this.name),
							},
							header:{
								'Content-Type':'application/x-www-form-urlencoded' //请求头信息
							},
							success:(res)=>{
								this.showSuccess("验证码发送成功");
							},
							fail:()=> {
								console.log("调用接口失败")
							}
						})
						this.Qztime ='60';
						this.Qztext = 's后获取';
						this.QzyzmStare = true;
						var interval = setInterval(() =>{
								--this.Qztime
							},1000)
						setTimeout(()=>{
							clearInterval(interval)
							this.Qztext = '获取验证码'
							this.Qztime = ''
							this.QzyzmStare = false
						},60000)
					}
				}	
     }
	}
</script>

<style>
.login_from{ 
		width: 100%; 
		height: auto; 
		box-sizing: border-box; 
		padding: 20upx 8%; 
	}
	
	.login_from_input{
		width: 100%; 
		height:auto; 
		display: flex; 
		flex-direction: row; 
		justify-content: space-between; 
		align-items: center; 
		border-bottom: 1px #eee solid; 
		padding: 40upx 0px; 
		margin: 0px auto;  
	}
	
	.login_from_name{ width: 20%; }
	.login_from_fun{ 
		width: 80%; 
		display: flex; 
		flex-direction: row; 
		justify-content: flex-end; 
		align-items: center;  
	}
	.login_from_fun input{ 
		width: 100%; 
		text-align: left; 
		font-size: 14px;  
	}
	
	.login_from_dl{ 
		width: 100%; 
		height: 50px; 
		line-height: 50px; 
		margin-top: 150upx;   
	}
	.login_from_dl button{ 
		width: 100%; 
		height: 50px; 
		line-height: 50px; 
		background: #2979ff; 
		color: #fff; 
		border-radius: 0px; 
	}
	.regFrom_tom_yzlabel{
		width: 60%; 
		text-align: right; 
	}
</style>
